home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-07-18 | 4.5 KB | 195 lines |
- /*
- * @(#)ISAPIConnectionHandler.java 1.7 97/07/16
- *
- * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
- *
- * This software is the confidential and proprietary information of Sun
- * Microsystems, Inc. ("Confidential Information"). You shall not
- * disclose such Confidential Information and shall use it only in
- * accordance with the terms of the license agreement you entered into
- * with Sun.
- *
- * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
- * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
- * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
- * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
- * THIS SOFTWARE OR ITS DERIVATIVES.
- *
- * CopyrightVersion 1.0
- */
-
- package sun.servlet.isapi;
-
- import javax.servlet.*;
- import java.net.*;
- import java.io.*;
- import sun.servlet.ServletConnection;
- import sun.servlet.http.*;
-
- /**
- * This class represents a servlet connection handler in the servlet server.
- *
- * @version 1.7, 07/16/97
- * @author Jongyoon Lee
- */
- class ISAPIConnectionHandler implements ServletConnection {
- /**
- * The http version number.
- */
- public static final String HTTP_VERSION = "HTTP/1.0";
-
- /**
- * The server for this handler.
- */
- protected ServletServer server;
-
- /**
- * The servlet request.
- */
- protected final ISAPIRequest req = new ISAPIRequest();
-
- /**
- * The servlet response.
- */
- protected final ISAPIResponse res = new ISAPIResponse();
-
- /**
- * The current ISAPI connection.
- */
- protected ISAPIConnection conn;
-
- /**
- * Creates a new handler for this specified server.
- */
- protected ISAPIConnectionHandler(ServletServer server) {
- this.server = server;
- conn = new ISAPIConnection();
- }
-
- /**
- * Runs the connection handler
- */
- public void run() {
- handleConnection(conn);
- }
-
- void init(long ecb) {
- conn.reset();
- conn.init(ecb);
- }
-
- /**
- * Handles a single connection from the client.
- * @param conn the ISAPI connection
- */
- protected void handleConnection(ISAPIConnection conn) {
- req.init(conn);
- res.init(conn);
- try {
- //res.setProtocol(HTTP_VERSION);
- //res.setHeader("Server", req.getServerName());
- // no keep-alive for now
- res.setKeepAlive(false);
- sendResponse(req, res);
- } catch (Throwable e) {
- if (res.getTotalBytes() == 0) {
- try {
- res.sendError(res.SC_INTERNAL_SERVER_ERROR);
- } catch (IOException ee) {
- // eat it!
- }
- }
- } finally {
- try {
- res.finish();
- } catch (IOException ee) {
- // eat it!
- }
- }
- }
-
-
- /**
- * Sends a response to the client.
- * @ServletException thrown if the servlet throws an exception in service.
- */
- protected void sendResponse(ISAPIRequest req, ISAPIResponse res)
- throws ServletException, IOException
- {
- String name = req.getServletPath();
- if (name != null) {
- // invoke servlet
- name = name.substring(1);
- int index;
- if ((index = name.indexOf('/')) != -1) {
- name = name.substring(index + 1);
- }
- if ((index = name.indexOf('/')) != -1) {
- name = name.substring(0, index);
- }
- Servlet s = server.getServlet(name);
- if (s != null) {
- s.service(req, res);
- return;
- }
- }
- res.sendError(res.SC_NOT_FOUND);
- }
-
- // ServletConnection interface
-
- /**
- * Returns the host name of the server that received the request.
- */
- public String getServerName() {
- return req.getServerName();
- }
-
- /**
- * Returns the port number on which this request was received.
- */
- public int getServerPort() {
- return req.getServerPort();
- }
-
- /**
- * Returns the fully qualified host name of the agent that sent the
- * request.
- */
- public String getRemoteHost() {
- return req.getRemoteHost();
- }
-
- /**
- * Returns the IP address of the agent that sent the request.
- */
- public String getRemoteAddr() {
- return req.getRemoteAddr();
- }
-
- /**
- * Returns the specified path translated to a real path.
- */
- public String getRealPath(String path) {
- return req.getRealPath(path);
- }
-
- /**
- * Returns an input stream for reading from the connection.
- */
- public InputStream getInputStream() throws IOException {
- return req.getInputStream();
- }
-
- /**
- * Returns an output stream for writing to the connection.
- */
- public OutputStream getOutputStream() throws IOException {
- return res.getOutputStream();
- }
- }
-
-
-
-